package com.hup.timer.view;

import com.hup.timer.model.TimerConfig;
import com.hup.timer.mvp.MainMvp.MainPresenter;
import com.hup.timer.view.dialog.AppConfigDialog;
import com.hup.timer.view.dialog.SyncTimeDialog;
import com.hup.utils.commons.start.AppIcon;
import com.hup.utils.commons.start.BaseAppTray;
import com.hup.utils.commons.start.SimpleExitHandler;
import com.hup.utils.commons.start.SpringContext;
import com.hup.utils.commons.start.StartupInfo;
import com.hup.utils.commons.start.VersionConfig;
import com.hup.utils.swing.FrameUtil;
import com.hup.utils.swing.FrameUtil.VisibleListener;
import com.hup.utils.swing.frame.HelpDialog;

import javax.swing.JCheckBoxMenuItem;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author hugan
 * @date 2020/1/10
 */
public class AppTray extends BaseAppTray {

    private static final String CMD_HELP = "帮助";
    private static final String CMD_MOUSE_PENETRATION = "鼠标穿透";
    private static final String CMD_SYNC_TIME = "同步网络时间";
    private static final String CMD_CONFIG = "设置";

    private static final String TIP_SHOWING = StartupInfo.getApplicationName() + ":展示中";
    private static final String TIP_HIDED = StartupInfo.getApplicationName() + ":已隐藏";

    private final MainPresenter presenter;
    private TimerWindow window;

    public AppTray(TimerWindow window) {
        super(SpringContext.getBean(AppIcon.class).getImage(), TIP_SHOWING);
        this.window = window;
        presenter = SpringContext.getBean(MainPresenter.class);
        setPopupMenu(getMenu(), new MyActionListener());
        setTrayListener(new TrayListener());

        FrameUtil.addVisibleListener(window, new VisibleListener() {
            @Override
            public void onShown() {
                setToolTip(TIP_SHOWING);
            }

            @Override
            public void onHidden(boolean dispose) {
                setToolTip(TIP_HIDED);
            }
        });
    }

    private SimpleMenu getMenu() {
        JCheckBoxMenuItem menuMousePenetration;
        SimpleMenu menu = new SimpleMenu();
        menu.addHeader(SpringContext.getBean(VersionConfig.class).version)
                .addMenu(CMD_HELP)
                .addSeparator0()
                .addMenu(menuMousePenetration = new JCheckBoxMenuItem(CMD_MOUSE_PENETRATION))
                .addSeparator0()
                .addMenu(CMD_SYNC_TIME)
                .addMenu(CMD_CONFIG)
                .addSeparator0()
                .addExit();
        menuMousePenetration.setSelected(presenter.getConfig().mousePenetration);
        return menu;
    }

    private static class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            switch (e.getActionCommand()) {
                case CMD_HELP:
                    HelpDialog.builder()
                            .minSize(new Dimension(350, 500))
                            .doShow(null, "/help/TimerWindow.html", null);
                    break;
                case CMD_MOUSE_PENETRATION:
                    boolean selected = ((JCheckBoxMenuItem) e.getSource()).isSelected();
                    MainPresenter presenter = SpringContext.getBean(MainPresenter.class);
                    TimerConfig timerConfig = presenter.getConfig();

                    timerConfig.setMousePenetration(selected);
                    timerConfig.save();
                    presenter.getTimerWindow().doUpdateMousePenetration();
                    break;
                case CMD_SYNC_TIME:
                    SyncTimeDialog.doShow();
                    break;
                case CMD_CONFIG:
                    AppConfigDialog.doShow();
                    break;
            }
        }
    }

    private class TrayListener implements TrayActionListener {
        @Override
        public void beforeShowMenu() {

        }

        @Override
        public void onClickTray() {
            boolean curVisible = window.isVisible();
            presenter.beforeSwitchWindowVisible(!curVisible);
            if (curVisible) {
                window.dispose();
            } else {
                window.setVisible(true);
                window.doUpdateMousePenetration();
            }
        }

        @Override
        public void onExit(BaseAppTray appTray) {
            SpringContext.getBean(SimpleExitHandler.class).doExit();
        }
    }

}
